package cn.niit.controller;

import cn.niit.entity.Classes;
import cn.niit.entity.Department;
import cn.niit.entity.Major;
import cn.niit.entity.Student;
import cn.niit.entity.query.StudentQuery;
import cn.niit.service.ClassService;
import cn.niit.service.DepartmentService;
import cn.niit.service.MajorService;
import cn.niit.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生表(Student)表控制层
 *
 * @author makejava
 * @since 2021-11-14 16:07:43
 */
@Controller
@RequestMapping("/student")

public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;
@Resource
private DepartmentService departmentService;
@Resource
private MajorService majorService;
@Resource
private ClassService classService;

    @GetMapping("/query")
    public String queryByStudent(Model model, StudentQuery studentQuery) {
        PageInfo<Student> studentPageInfo = studentService.queryByStudent(studentQuery);
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();
        List<Classes> classesList=classService.queryAllClass();
        model.addAttribute("classes",classesList);
        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("page", studentPageInfo);
        return "/student/student";
    }
    @RequiresPermissions("user:user:select")
    @GetMapping("/select")
    public String select(Model model,StudentQuery studentQuery){
        PageInfo<Student> studentPageInfo=studentService.select(studentQuery);
        model.addAttribute("page",studentPageInfo);
        return "/student/student";
    }

    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        model.addAttribute("student", studentService.queryStudentById(id));
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();
        List<Classes> classesList=classService.queryAllClass();
        model.addAttribute("classesList",classesList);
        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        return "/student/editStudent";
    }

    @PostMapping("/edit")
    public String edit(Student student) {
        StudentQuery studentQuery = new StudentQuery();
        Integer id = student.getId();
        studentQuery.setName(student.getName());
        PageInfo<Student> studentPageInfo = studentService.queryByStudentName(studentQuery);
        boolean b;
        if (id != null) {
            b = studentService.editStudent(student);
        } else {
            b = studentService.addStudent(student);
        }
        return "redirect:/student/query";
    }

    @GetMapping("/update")
    public String update(Model model) {
        List<Department> departmentList=departmentService.queryAllDepartmentList();
        List<Major> majorList=majorService.queryAllMajorList();
        List<Classes> classesList=classService.queryAllClass();
        model.addAttribute("classesList",classesList);
        model.addAttribute("majorList",majorList);
        model.addAttribute("departmentList",departmentList);
        Student student = new Student();
        model.addAttribute("student", student);
        return "/student/editStudent";
    }

    @GetMapping("/delete/{id}")
    public String deleteStudentById(@PathVariable("id") Integer id) {
        boolean b = studentService.deleteStudentById(id);
        return "redirect:/student/query";
    }

}